package com.sita.bluetactivity.TripFragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bluetactivity.Base.BaseActivity;
import com.sita.bluetactivity.R;

/**
 * Created by Jason on 2016/10/9.
 */

public class VehicleTripAdapter extends RecyclerView.Adapter<VehicleTripAdapter.MyViewHoler>  {
    private TripBackBean backBean;
    private Context context;
    private TripListener clicklistener;

    public VehicleTripAdapter(Context context,TripListener listener) {
        backBean = new TripBackBean();
        this.context = context;
        clicklistener = listener;
    }
    public void setData(TripBackBean tripBackBean){
        backBean = tripBackBean;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_vehicletrip,parent,false);
        return new MyViewHoler(view);
    }

    @Override
    public void onBindViewHolder(MyViewHoler holder, int position) {
     if (holder!=null){
         holder.startTime.setText("开始时间："+backBean.getData().get(position).getBegintimestr());
         holder.endTime.setText("结束时间："+backBean.getData().get(position).getEndtimestr());
         holder.allTrip.setText("总里程："+String.valueOf(backBean.getData().get(position).getDistance()/1000).substring(0,3)+"km");
         holder.aveSpeed.setText("平均速度："+backBean.getData().get(position).getSpeed()+"km/h");
         holder.position = position;
     }
    }
    @Override
    public int getItemCount() {
        if (backBean.getData()==null){
            return 0;
        }
        else {
            return backBean.getData().size();
        }
    }
    class MyViewHoler extends RecyclerView.ViewHolder{
        private TextView startTime,endTime,allTrip,aveSpeed;
        private int position;
        private RelativeLayout allLayout;
        public MyViewHoler(View itemView) {
            super(itemView);
            startTime = (TextView) itemView.findViewById(R.id.start_time);
            endTime = (TextView) itemView.findViewById(R.id.end_time);
            allTrip = (TextView) itemView.findViewById(R.id.all_trip);
            aveSpeed = (TextView) itemView.findViewById(R.id.average_speed);
            allLayout = (RelativeLayout) itemView.findViewById(R.id.all_layout);
            allLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clicklistener.AllTripClick(position);

                                    }
            });

        }
    }
}
